//
//  MyBottomView.m
//  Rabbit
//
//  Created by 阿帕 on 2018/11/6.
//  Copyright © 2018年 GCY. All rights reserved.
//

#import "MyBottomView.h"

@interface MyBottomView ()
{
    NSInteger _vehicleManageTempTag;
    NSInteger _driverManageTempTag;
}

@property(nonatomic, strong)UIImageView *overdueImage;
@property(nonatomic, strong)UILabel     *merchLabel;

@end

@implementation MyBottomView

#pragma mark - Intial
- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
//        [self setUpUI];
    }
    
    return self;
}

- (void)dealloc {
    [KNotificationCenter removeObserver:self name:LOGIN_SUCCESS object:nil];
    [KNotificationCenter removeObserver:self name:LOGIN_LOGOUT object:nil];
    [KNotificationCenter removeObserver:self name:@"AuthOverdue" object:nil];
    [KNotificationCenter removeObserver:self name:@"AuthOverdueAutonym" object:nil];
}

- (void)layoutSubviews {
    
    [self setUpUI];
}

- (void)setUpUI {
    
    self.backgroundColor = [UIColor whiteColor];
    
    [KNotificationCenter addObserver:self selector:@selector(refreshHomePageData:) name:LOGIN_SUCCESS object:nil];
    [KNotificationCenter addObserver:self selector:@selector(refreshLogout:) name:LOGIN_LOGOUT object:nil];
    [KNotificationCenter addObserver:self selector:@selector(authOverdue:) name:@"AuthOverdue" object:nil];
    [KNotificationCenter addObserver:self selector:@selector(AuthOverdueAutonym:) name:@"AuthOverdueAutonym" object:nil];
        
    [self layoutMenuListView];
}

- (void)layoutMenuListView {
    
    //0独立司机 1车队 2车队司机
    
    NSInteger driverType = [[UserDefaultUtil valueForKey:DRIVER_TYPE] integerValue];
    NSDictionary *userInfo = [UserDefaultUtil valueForKey:Login_Branch];
    
    NSMutableArray<NSString *> *muTitles = [[NSMutableArray alloc] init];
    NSMutableArray *muImages = [[NSMutableArray alloc] init];
    
    if (self) {
        [self removeAllSubviews];
    }

    if (driverType == 0) {
        //0独立司机
        NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithObjects:
                                            @"实名认证",@"司机认证信息", @"车辆管理", @"税务认证", @"加入车队", @"已加入车队列表", @"我的车源信息", @"保单列表", @"车辆维修", @"电子合同", @"投诉记录",  @"运费统计",  @"车辆年检业务", @"用户评价", @"投诉意见反馈", @"关于我们", nil
                                        ];
        NSMutableArray *muImageArray = [[NSMutableArray alloc] initWithObjects:
        @"shimingIcom",@"gcy_icon_my_index0", @"gcy_icon_my_carManage", @"gcy_icon_my_face", @"gcy_icon_my_index1", @"gcy_icon_my_index2", @"gcy_icon_my_index11", @"gcy_icon_my_index12", @"gcy_icon_my_index10", @"gcy_icon_my_index3", @"gcy_icon_my_index4",  @"gcy_icon_my_index6",@"gcy_icon_my_index12", @"gcy_icon_my_index7", @"gcy_icon_my_index8", @"gcy_icon_my_index9",nil
        ];
        muTitles = mutableArray;
        muImages = muImageArray;
        
//        if (_taxPlatform == 2) {
//            [muTitles insertObject:@"税务认证" atIndex:1];
//            [muImages insertObject:@"gcy_icon_my_face" atIndex:1];
//        }
        
//        if (Identity_Driver) {
//            NSDictionary *userInfo = [UserDefaultUtil valueForKey:Login_Branch];
//            if ([userInfo isKindOfClass:[NSDictionary class]]) {
//                NSString *paymentType = [userInfo objectForKey:@"paymentType"];
//                [paymentType isEqualToString:@"arpa"] ? nil : [muTitles insertObject:@"免验授权" atIndex:3];
//                [muImages insertObject:@"gcy_icon_my_mianyansq" atIndex:3];
//            }
//        }
        
    } else if (driverType == 1) {
        //1车队 12
        NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithObjects:
                                            @"实名认证",@"车队认证信息", @"司机管理", @"司机加入", @"车辆管理", @"车辆维修", @"保单列表", @"电子合同", @"投诉记录", @"运费统计",@"车辆年检业务", @"用户评价", @"投诉意见反馈", @"关于我们",  nil
                                        ];
        NSMutableArray *muImageArray = [[NSMutableArray alloc] initWithObjects:
                                            @"shimingIcom",@"gcy_icon_my_index0", @"gcy_icon_my_sijimanage", @"gcy_icon_my_sijijiaru", @"gcy_icon_my_carManage", @"gcy_icon_my_index10", @"gcy_icon_my_index12", @"gcy_icon_my_index3", @"gcy_icon_my_index4", @"gcy_icon_my_index6",@"gcy_icon_my_index12", @"gcy_icon_my_index7", @"gcy_icon_my_index8", @"gcy_icon_my_index9",  nil
        ];
        muTitles = mutableArray;
        muImages = muImageArray;
        
    } else if (driverType == 2) {
        //2车队司机 11
        NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithObjects:
                                            @"实名认证",@"司机认证信息", @"税务认证", @"已加入车队列表", @"扫码绑车", @"我的车源信息", @"保单列表", @"电子合同", @"车辆维修", @"投诉记录", @"运费统计",@"车辆年检业务", @"用户评价", @"投诉意见反馈", @"关于我们", nil
                                        ];
        NSMutableArray *muImageArray = [[NSMutableArray alloc] initWithObjects:
                                            @"shimingIcom",@"gcy_icon_my_index0", @"gcy_icon_my_face", @"gcy_icon_my_index2", @"gcy_icon_my_span", @"gcy_icon_my_index11", @"gcy_icon_my_index12", @"gcy_icon_my_index3", @"gcy_icon_my_index10", @"gcy_icon_my_index4",  @"gcy_icon_my_index6",@"gcy_icon_my_index12", @"gcy_icon_my_index7", @"gcy_icon_my_index8", @"gcy_icon_my_index9", nil
        ];
        muTitles = mutableArray;
        muImages = muImageArray;
        
//        if (_taxPlatform == 2) {
//            [muTitles insertObject:@"税务认证" atIndex:1];
//            [muImages insertObject:@"gcy_icon_my_face" atIndex:1];
//        }
        
        if (Identity_Driver) {
            [muTitles insertObject:@"免验授权" atIndex:3];
            [muImages insertObject:@"gcy_icon_my_mianyansq" atIndex:3];
        }
        
    }
    
    /*
     22-07-12新增
     paymentType=arpa_2 且 paymentChannel=6时展示此菜单
     */
    if ([userInfo[@"paymentType"] isEqualToString:@"arpa_2"] && [userInfo[@"paymentChannel"] intValue] == 6 && !TOKEN_ISEMPTY) {
        [muTitles insertObject:@"提现协议签约" atIndex:10];
        [muImages insertObject:@"gcy_icon_my_tixianxieyi" atIndex:10];
    }
    
    if ([UserDefaultUtil valueForKey:Login_Branch] != NULL) {
        NSDictionary *paymentInfo = [UserDefaultUtil valueForKey:Login_Branch];
        if ([paymentInfo isKindOfClass:NSDictionary.class] && [[paymentInfo valueForKey:@"paymentType"] isEqual:@"arpa"]) {
            // 无网商功能或云企付
            [muTitles insertObject:@"交易记录" atIndex:2];
            [muImages insertObject:@"gcy_icon_my_jiaoyijilu" atIndex:1];

            [muTitles insertObject:@"银行卡信息" atIndex:3];
            [muImages insertObject:@"gcy_icon_my_index5" atIndex:2];
        }
    }
    
    for (int i=0; i<muTitles.count; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.backgroundColor = [UIColor whiteColor];
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        button.tag = 150+i;
        [button setTitleColor:Dark_TextColor forState:UIControlStateNormal];
//        [button setTitle:muTitles[i] forState:UIControlStateNormal];
//        [button setImage:[UIImage changeImageSize:image(muImages[i]) AndSize:CGSizeMake(18, 18)] forState:UIControlStateNormal];
//        button.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
//        [[button imageView] setContentMode:UIViewContentModeScaleAspectFit];
        [button setImage:image(muImages[i]) forState:UIControlStateNormal];
        button.titleLabel.font = font(14);
        [button addTarget:self action:@selector(buttonTypeClick:) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake(30, 45*i, ScreenW-45, 45);
        [self addSubview:button];
        
        UILabel *tLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 0, 200, button.height)];
        tLabel.textColor = Dark_TextColor;
        tLabel.text = muTitles[i];
        tLabel.font = font(14);
        tLabel.tag = 100 + i;
        
        [button addSubview:tLabel];
        
        if (i>0) {
            UIView *line = [[UIView alloc] initWithFrame:CGRectMake(15, 45*i, ScreenW-30, 1)];
            line.backgroundColor = [UIColor cy_colorWithHexString:@"#E4E9ED"];
            [self addSubview:line];
        }
        
        UIImageView *arrow = [[UIImageView alloc] initWithFrame:CGRectMake(button.cy_width - 10, 17, 6, 11)];
        arrow.image = image(@"gcy_icon_hd_rarrow");
        [button addSubview:arrow];
        
        [button layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:15];
        
        if ([muTitles[i] isEqual:@"免验授权"]) {
            _merchLabel = [[UILabel alloc] initWithFrame:CGRectMake(button.width - 110, 0, 80, button.height)];
            _merchLabel.tag = 3000;
            _merchLabel.textColor = Dark_TextColor;
            _merchLabel.font = font(14);
            _merchLabel.textAlignment = NSTextAlignmentRight;
            [button addSubview:_merchLabel];
        }
        
        if (i == 1 || i == 0) {
            CGSize tSize = [DCSpeedy dc_calculateTextSizeWithText:tLabel.text WithTextFont:14 WithMaxW:1000];
            UIImageView *icon = [[UIImageView alloc] initWithFrame:CGRectMake(tLabel.left + tSize.width + 5, 10.5f, 24, 24)];
            icon.contentMode = UIViewContentModeScaleAspectFit;
            icon.tag = 2000;
            [button addSubview:icon];
        }
        
        if ([muTitles[i] isEqualToString:@"司机管理"] || [muTitles[i] isEqualToString:@"车辆管理"]) {
            CGSize tSize = [DCSpeedy dc_calculateTextSizeWithText:tLabel.text WithTextFont:14 WithMaxW:1000];
            UIImageView *icon = [[UIImageView alloc] initWithFrame:CGRectMake(tLabel.left + tSize.width + 5, 10.5f, 24, 24)];
            [icon setImage:[UIImage imageNamed:@"gcy_icon_my_yidaoqi"]];
            icon.contentMode = UIViewContentModeScaleAspectFit;
            icon.tag = 2000 + i;
            if ([muTitles[i] isEqualToString:@"司机管理"]) {
                _driverManageTempTag = i;
            } else {
                _vehicleManageTempTag = i;
            }
            [button addSubview:icon];
            icon.hidden = YES;
        }
        
    }
    
    //最后计算菜单的高度
    if (self.delegate && [self.delegate respondsToSelector:@selector(CalculateMenuListHeight:)]) {
        [self.delegate CalculateMenuListHeight:muTitles.count * 45];
    }
    
    UIView *divider = [[UIView alloc] initWithFrame:CGRectMake(0, self.height-85, ScreenW, 5)];
    divider.backgroundColor = BackgroundColor;
    [self addSubview:divider];

    UIView *btmView = [[UIView alloc] initWithFrame:CGRectMake(0, self.height-120, ScreenW, 120)];
    [self addSubview:btmView];

    UIButton *phoneButton = [[UIButton alloc] init];
    phoneButton.tag = 100;
    [phoneButton setFrame:CGRectMake(0,10, ScreenW, 30)];
    [phoneButton addTarget:self action:@selector(phoneButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [phoneButton setBackgroundColor:[UIColor whiteColor]];
    [phoneButton setTitleColor:Light_TextColor forState:UIControlStateNormal];
    phoneButton.titleLabel.font = font(14);
    NSString *tel = [UserDefaultUtil valueForKey:CustomerTel];
    if ([DCSpeedy isBlankString:tel] == YES) {
        tel = Default_Tel;
    }
    [phoneButton setTitle:CJStringWithFormat(@"全国24小时服务电话 %@", tel) forState:(UIControlStateNormal)];
    
    UIButton *qgPhoneButton = [[UIButton alloc] init];
    qgPhoneButton.tag = 101;
    [qgPhoneButton setFrame:CGRectMake(0, CGRectGetMaxY(phoneButton.frame) + 10, ScreenW, 30)];
    [qgPhoneButton addTarget:self action:@selector(phoneButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [qgPhoneButton setBackgroundColor:[UIColor whiteColor]];
    [qgPhoneButton setTitleColor:Light_TextColor forState:UIControlStateNormal];
    qgPhoneButton.titleLabel.font = font(14);
    NSString *qgtel = [UserDefaultUtil valueForKey:CustomerLine];
    if ([DCSpeedy isBlankString:qgtel] == YES) {
        qgtel = Default_Tel;
    }
    [qgPhoneButton setTitle:CJStringWithFormat(@"集团客服中心投诉热线 %@", qgtel) forState:(UIControlStateNormal)];
    
    UIButton *cjPhoneButton = [[UIButton alloc] init];
    cjPhoneButton.tag = 102;
    [cjPhoneButton setFrame:CGRectMake(0, CGRectGetMaxY(phoneButton.frame) + 10, ScreenW, 30)];
    [cjPhoneButton addTarget:self action:@selector(phoneButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [cjPhoneButton setBackgroundColor:[UIColor whiteColor]];
    [cjPhoneButton setTitleColor:Light_TextColor forState:UIControlStateNormal];
    cjPhoneButton.titleLabel.font = font(14);
    [cjPhoneButton setTitle:CJStringWithFormat(@"车辆年检服务电话 %@", @"18314169971") forState:(UIControlStateNormal)];
    
    [btmView addSubview:phoneButton];
//    [btmView addSubview:qgPhoneButton];
    [btmView addSubview:cjPhoneButton];
}
- (void)phoneButtonAction:(UIButton *)btn{
    NSString *tel;
    if (btn.tag == 100) {
        tel = [UserDefaultUtil valueForKey:CustomerTel];
        if ([DCSpeedy isBlankString:tel] == YES) {
            tel = Default_Tel;
        }
    }else  if (btn.tag == 101) {
        tel = [UserDefaultUtil valueForKey:CustomerLine];
        if ([DCSpeedy isBlankString:tel] == YES) {
            tel = Default_Tel;
        }
    }else{
        tel = @"18314169971";
    }
    CALL_PHONE(tel);
}
- (void)setAuthStatus:(NSInteger)authStatus {
    
    //    authStatus 审核状态:0未审核/1审核中/2审核未通过/3审核通过
    _authStatus = authStatus;
    
    UIButton *button = [self viewWithTag:150];
    UIImageView *icon = [button viewWithTag:2000];

//    if (authStatus == 2) {
//        icon.image = image(@"gcy_icon_my_yibohui");
//    } else if (_authStatus == 3) {
//        icon.image = image(@"gcy_icon_my_yirenzheng");
//    } else {
//        icon.image = image(@"gcy_icon_my_yiguoqi");
//    }
    
//    if (authStatus == 0) {
//        //        [button setTitle:@"认证信息(未审核)" forState:UIControlStateNormal];
//    } else if (authStatus == 1) {
//        //        [button setTitle:@"认证信息(审核中)" forState:UIControlStateNormal];
//    } else if (authStatus == 2) {
//        //        [button setTitle:@"认证信息(审核未通过)" forState:UIControlStateNormal];
//    } else if (authStatus == 3) {
//        icon.image = image(@"gcy_icon_my_yirenzheng");
//    }
    
}

- (void)setDriverCertificateHasDue:(NSInteger)driverCertificateHasDue {
    _driverCertificateHasDue = driverCertificateHasDue;
    
    if (Identity_CarCaptain) {
        UIImageView *icon = [[self viewWithTag:150+_driverManageTempTag] viewWithTag:2000+_driverManageTempTag];
        icon.hidden = driverCertificateHasDue == 0 ? YES : NO;
    }
}

- (void)setVehicleCertificateHasDue:(NSInteger)vehicleCertificateHasDue {
    _vehicleCertificateHasDue = vehicleCertificateHasDue;
    
    if (Identity_CarCaptain) {
        UIImageView *icon = [[self viewWithTag:150+_vehicleManageTempTag] viewWithTag:2000+_vehicleManageTempTag];
        icon.hidden = vehicleCertificateHasDue == 0 ? YES : NO;
    }
}

- (void)refreshLogout:(NSNotification *)notification {
    UIImageView *vehicleIcon = [[self viewWithTag:150+_vehicleManageTempTag] viewWithTag:2000+_vehicleManageTempTag];
    if (vehicleIcon) {
        vehicleIcon.hidden = YES;
    }
    
    UIImageView *driverIcon = [[self viewWithTag:150+_driverManageTempTag] viewWithTag:2000+_driverManageTempTag];
    if (driverIcon) {
        driverIcon.hidden = YES;
    }
}

- (void)setMerchSign:(NSInteger)merchSign {
    
    _merchSign = merchSign;
    
    if (Identity_Driver) {
        
        if (merchSign == 0) {
            // 授权状态，0待授权（默认），1授权中，2已授权，3授权失败
            _merchLabel.text = @"待授权";
        } else if (merchSign == 1) {
            _merchLabel.text = @"授权中";
        } else if (merchSign == 2) {
            _merchLabel.text = @"已授权";
        } else if (merchSign == 3) {
            _merchLabel.text = @"授权失败";
        }
    }
    
}

- (void)setIsTax:(NSInteger)isTax {
//    _isTax = isTax;
//    if (isTax == 1) {
//        [self layoutMenuListView];
//    }
}

- (void)setTaxPlatform:(NSInteger)taxPlatform {
    _taxPlatform = taxPlatform;
    if (taxPlatform == 2) {
        [self layoutMenuListView];
    }
}

- (void)authOverdue:(NSNotification *)notification {
    UIImageView *icon = [[self viewWithTag:151] viewWithTag:2000];
    
    if (![notification.object isKindOfClass:[NSString class]]) {
        if (!notification.object)  {
            // 退出登录隐藏审核状态/免验授权状态
            icon.hidden = YES;
            _merchLabel.hidden = YES;
        }
        return;
    }
    
    if (_authStatus == 2) { // 驳回
        icon.image = image(@"gcy_icon_my_yibohui");
    } else {
        BOOL isPass = [notification.object boolValue];
        if (!isPass) { // 有证照过期
            icon.image = image(@"gcy_icon_my_yidaoqi");
            icon.hidden = NO;
            _merchLabel.hidden = NO;
        } else {
            if (_authStatus == 3) {
                icon.image = image(@"gcy_icon_my_yirenzheng");
            } else {
                icon.image = image(@"gcy_icon_my_yiguoqi");
            }
        }
    }

}
- (void)AuthOverdueAutonym:(NSNotification *)notification{
    UIImageView *icon = [[self viewWithTag:150] viewWithTag:2000];
    
    if (![notification.object isKindOfClass:[NSString class]]) {
        if (!notification.object)  {
            // 退出登录隐藏审核状态/免验授权状态
            icon.hidden = YES;
            _merchLabel.hidden = YES;
        }
        return;
    }
    icon.image = image(@"gcy_icon_my_yirenzheng");
//    if (_authStatus == 2) { // 驳回
//        icon.image = image(@"gcy_icon_my_yibohui");
//    } else {
//        BOOL isPass = [notification.object boolValue];
//        if (!isPass) { // 有证照过期
//            icon.image = image(@"gcy_icon_my_yidaoqi");
//            icon.hidden = NO;
//            _merchLabel.hidden = NO;
//        } else {
//            if (_authStatus == 3) {
//                icon.image = image(@"gcy_icon_my_yirenzheng");
//            } else {
//                icon.image = image(@"gcy_icon_my_yiguoqi");
//            }
//        }
//    }

    
}
- (void)refreshHomePageData:(NSNotification *)notification {
    
    [self layoutMenuListView];
}

- (void)buttonTypeClick:(UIButton *)button {
    UILabel *tLab = [self viewWithTag:button.tag-50];
    if (self.delegate && [self.delegate respondsToSelector:@selector(MyBottomViewBtnClick:andBtnTitle:)]) {
        [self.delegate MyBottomViewBtnClick:button.tag andBtnTitle:tLab.text];
    }
}

@end
